package edu.geo.service;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.geo.containers.Json;
import edu.geo.core.GeoHandler;
import edu.geo.db.conn.DBConnection;
import edu.geo.protocol.JsonProtocol;

/**
 * Servlet implementation class GeoinformaticsService
 */
public class GeoinformaticsService extends HttpServlet {
	
	public static final String GEO_DB = "geodb";
	
	DBConnection dbConnection = new DBConnection(GEO_DB);
		
	/**
	 * 
	 */
	private static final long serialVersionUID = -3846619292735344433L;
	private GeoHandler gHandler;
	/**
     * Default constructor. 
     */
    public GeoinformaticsService() {
       gHandler = new GeoHandler(dbConnection); 
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 processRequest(request,response);
	}
		   
   
	   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
	        response.setContentType("text/html;charset=UTF-8");
	        JsonProtocol jProtocol = new JsonProtocol();
	        Json requestJson;
	        Json responseJson;
	        requestJson= jProtocol.read(request.getReader());
	        responseJson = gHandler.geoHandler(requestJson);
	        jProtocol.write(response.getWriter(), responseJson);
	    }

}
